const app = getApp();
Component({
  externalClasses: ['video-class'],
  /**
   * 组件的属性列表
   */
  properties: {
    src: {
      type: String,
      observer: function(newVal, oldVal) {
        if (newVal && /^WSVOD/.test(newVal)) {
          this.setData({
            isVod: true,
          }, () => this.getVodUrl());
        }
      },
    },
    duration: Number,
    controls: {
      type: Boolean,
      value: true,
    },
    danmuList: Array,
    danmuBtn: {
      type: Boolean,
      value: false,
    },
    enableDanmu: {
      type: Boolean,
      value: false,
    },
    autoplay: {
      type: Boolean,
      value: false,
    },
    loop: {
      type: Boolean,
      value: false,
    },
    muted: {
      type: Boolean,
      value: false,
    },
    initialTime: {
      type: Number,
      value: 0,
    },
    pageGesture: {
      type: Boolean,
      value: false,
    },
    direction: {
      type: Number,
      value: -1,
    },
    showProgress: {
      type: Boolean,
      value: true,
    },
    showFullscreenBtn: {
      type: Boolean,
      value: true,
    },
    showPlayBtn: {
      type: Boolean,
      value: true,
    },
    showCenterPlayBtn: {
      type: Boolean,
      value: true,
    },
    enableProgressGesture: {
      type: Boolean,
      value: true,
    },
    objectFit: {
      type: String,
      value: 'contain',
    },
    poster: String,
    showMuteBtn: {
      type: Boolean,
      value: false,
    },
    title: String,
    playBtnPosition: {
      type: String,
      value: 'bottom',
    },
    enablePlayGesture: {
      type: Boolean,
      value: false,
    },
    autoPauseIfNavigate: {
      type: Boolean,
      value: true,
    },
    autoPauseIfOpenNative: {
      type: Boolean,
      value: true,
    },
    vslideGesture: {
      type: Boolean,
      value: false,
    },
    vslideGestureInFullscreen: {
      type: Boolean,
      value: false,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    isVod: false,
    tokenid: '',
  },

  ready() {
    this.triggerEvent('context', { context: wx.createVideoContext('video', this) });
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getVodUrl() {
      app.util.request({
        url: 'entry/wxapp/vod',
        data: {
          op: 'getByKey',
          key: this.data.src,
        },
        module: 'cm_vod',
        cachetime: 0,
        success: (res) => {
          if (res.data && res.data.errno == 0) {
            const data = res.data.data;
            this.setData({
              tokenid: data.tokenid,
              src: data.url,
            });
          }
          if (res.data && res.data.errno == 1) {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
            });
          }
        },
      });
    },
    onPlay(e) {
      this.triggerEvent('play', e.detail);
    },
    onPause(e) {
      this.triggerEvent('pause', e.detail);
    },
    onEnded(e) {
      this.triggerEvent('ended', e.detail);
    },
    onTimeupdate(e) {
      this.triggerEvent('timeupdate', e.detail);
      let currentTime = e.detail.currentTime;
      let duration = e.detail.duration;
      let pen = (currentTime / duration) * 100;
      if (this.data.isVod) {
        app.util.request({
          url: 'entry/wxapp/vod',
          data: {
            op: 'playLength',
            tokenid: this.data.tokenid,
            pencent: parseInt(pen),
          },
          module: 'cm_vod',
          cachetime: 0,
          success: (res) => {
            
          },
        });
      }
    },
    onFullscreenchange(e) {
      this.triggerEvent('fullscreenchange', e.detail);
    },
    onWaiting(e) {
      this.triggerEvent('waiting', e.detail);
    },
    onError(e) {
      this.triggerEvent('error', e.detail);
    },
    onProgress(e) {
      this.triggerEvent('progress', e.detail);
    },
  },
});
